<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PersonalController extends Controller
{

	/**
	 * 晒单评价
	 */
    public function getComments(Request $request)
    {
    	// echo "string";
    	// 通过session获取用户的id
    	$userid = $request->session()->get('id');
      	$date = DB::table('goodscomment')
      	->leftJoin('users','users.id','=','goodscomment.userid')
      	->leftJoin('orders','user_id','=','users.id')
      	->leftJoin('order_goods','orders.id','=','order_goods.order_id')
      	->leftJoin('goods','goodscomment.goodsid','=','goods.id')
      	->leftJoin('kucun','goods.id','=','kucun.gid')
      	->where('goodscomment.id',$userid)
      	->select('goodscomment.*','users.username','order_goods.goods_name','kucun.price','goods.image','goods.id as gid','orders.isComment','orders.id as order_id')
      	->get();
      	// dd($date);
      	$maxL = DB::table('orders')->where('user_id',$userid)->count();
      	// dd($maxL);
      	$plLength = DB::table('orders')->where('isComment',1)->count();
      	// dd($plLength);
      	// dd($date);
      	return view('personal.comments',['comments'=>$date,'maxL'=>$maxL,'plLength'=>$plLength]);
    }

    /**
     * 显示评价详情
     */
    public function getDes($id)
    {
    	// 通过传过来的评论id联查
      	$date = DB::table('goodscomment')
      	->leftJoin('recontents','recontents.comment_id','=','goodscomment.id')
      	->leftJoin('users','users.id','=','goodscomment.userid')
      	->leftJoin('goods','goodscomment.goodsid','=','goods.id')
      	->leftJoin('kucun','goods.id','=','kucun.gid')
      	->where('goodscomment.id',$id)
      	->select('goodscomment.*','users.username','goods.proname','goods.image','kucun.price','recontents.comment_id')
      	->get();
      	// dd($date);
      	foreach ($date as $key => $value) {

      		$username = $value->username;
      		$proname = $value->proname;
      		$commenttime = $value->commenttime;
      		$comment_id = $value->comment_id;
      		$gid = $value->goodsid;
      		$price = $value->price;
      		$image = $value->image;

      	}
      	$date1 = DB::table('recontents')
      	->leftJoin('goodscomment','recontents.comment_id','=','goodscomment.id')
      	->where('comment_id',$id)
      	->select('recontents.*')
      	->get();
      	// dd($date1);

      	// var_dump($res);var_dump($con);die;

      	
    	return view('personal.des',['username'=>$username,'proname'=>$proname,'commenttime'=>$commenttime,'comment_id'=>$comment_id,'date1'=>$date1,'gid'=>$gid,'price'=>$price,'image'=>$image]);
    }

    /**
     * 追加评论
     */
    public function postInsert(Request $request)
    {
    	// echo "string";
    	// 通过session获取用户的id
    	$userid = $request->session()->get('id');
    	$rec = $request->only(['content','comment_id']);
    	// dd($rec);
    	$date = DB::table('recontents')->insert($rec);
    	// dd($date);
    	if($date){
    		//成功 重定向 页面跳转到列表页
    		return redirect('/personal/des/'.$userid)->with('success','1');
    	}else{
    		//失败 重定向 回到当前页
    		return back()->with('error','0');
    	}
    }

    /**
     * 去评价页面
     */

    public function postTocomment(Request $request)
    {
    	// 获取传过来的商品id和订单id
    	$goodsid = $request->input('goodsid');
    	$orderid = $request->input('orderid');
    	// dd($goodsid);
    	// dd($orderid);
    	$goods = DB::table('order_goods')
    	->leftJoin('orders','order_goods.order_id','=','orders.id')
    	->select('order_goods.*','orders.isComment')
    	->where('order_goods.goods_id',$goodsid)
    	->get();
    	// dd($goods);
    	foreach ($goods as $k => $v) {
    		$isComment = $v->isComment;
    		$goodsid = $v->goods_id;
    	}
    	$sp = DB::table('kucun')->where('gid',$goodsid)->get();
    	// dd($sp);
    	foreach ($sp as $key => $value) {
    		$miaoshu = $value->miaoshu;
    		$size = $value->size;
    		$proname = $value->proname;
    		$price = $value->price;
    	}
    	$tp = DB::table('goods')->where('id',$goodsid)->get();
    	foreach ($tp as $kk => $vv) {
    		$img = $vv->image;
    	}
    	// dd($isComment);
    	// $orders = DB::table('orders')->where('id',$orderid)->update(['isComment'=>'1']);
    	// dd($orders);
    	// 返回视图页面
    	return view('personal.insert',['goods'=>$goods,'gid'=>$goodsid,'image'=>$img,'miaoshu'=>$miaoshu,'size'=>$size,'proname'=>$proname,'price'=>$price,'order_id'=>$orderid]);
    }

    /**
     * 插入新评论
     */
    public function postGocomment(Request $request)
    {
    	// 通过session获取用户的id
    	$userid = $request->session()->get('id');
      $odid = $request->input('order_id');
      $gid = $request->input('gid');
    	$content = $request->input('content');
      $commentt = date('Y-m-d H:i:s',time());

      $commentid = DB::table('goodscomment')
      ->insertGetId(['goodsid'=>$gid,'userid'=>$userid,'commenttime'=>$commentt]);
      
      // dd($commentid);
    	$date = DB::table('recontents')->insert(['content'=>$content,'comment_id'=>$commentid]);
      $xiug = DB::table('orders')->where('id',$odid)->update(['isComment'=>1]);
    	if($date){
    		//成功 重定向 页面跳转到列表页
    		return redirect('/personal/des/'.$commentid)->with('success','1');
    	}else{
    		//失败 重定向 回到当前页
    		return back()->with('error','0');
    	}
    }


     /**
	 * 前台退换货申请
	 */
    public function getReplace(Request $request)
    {
        // echo "string";
        // 通过session获取用户的id
        $userid = $request->session()->get('id');
        $date = DB::table('regoods')
        ->leftJoin('goods','regoods.gid','=','goods.id')
        ->leftJoin('users','users.id','=','regoods.uid')
        ->leftJoin('orders','users.id','=','orders.user_id')
        ->leftJoin('order_goods','orders.id','=','order_goods.order_id')
        ->where('regoods.uid',$userid)
        ->whereIn('orders.status',[3,4])
        ->whereIn('regoods.statuss',[0,1])
        ->select('regoods.statuss','goods.proname','goods.image','orders.order_code','orders.total','orders.order_time','orders.status','goods.id as gid','regoods.statuss')
        ->get();
        // dd($date);
        $maxTH = DB::table('regoods')->where('uid',$userid)->where('statuss',1)->count();
      	// dd($maxTH);
      	$maxHH = DB::table('regoods')->where('uid',$userid)->where('statuss',0)->count();
      	// dd($maxHH);
        return view('personal.regoods',['date'=>$date,'maxTH'=>$maxTH,'maxHH'=>$maxHH]);
    }
}
